package com.winter.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: Collection
 * @description: ArrayList遍历集合
 * @author: Mr.Liang
 * @create: 2018-01-05 16:46
 **/
public class ArrayListIterator {
    public static void main(String[] args) {
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            list.add(i);
        }
        iterator(list);
        random(list);
        foreach(list);
    }

    //      Iterator遍历
    public static void iterator(List<Object> list) {
        long startTime; //开始时间
        long endTime;   //结束时间
        startTime = System.currentTimeMillis();
        Iterator<Object> iterator = list.iterator();
        while (iterator.hasNext()) {
            iterator.next();
        }
        endTime = System.currentTimeMillis();
        long longTime = endTime - startTime;
        System.out.println("iterator:" + longTime + "ms");
    }

    //    通过索引遍历
    public static void random(List<Object> list) {
        Long startTime; //开始时间
        Long endTime;   //结束时间
        startTime = System.currentTimeMillis();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            list.get(i);
        }
        endTime = System.currentTimeMillis();
        long longTime = endTime - startTime;
        System.out.println("random:" + longTime + "ms");
    }

    //    for循环遍历
    public static void foreach(List<Object> list) {
        Long startTime; //开始时间
        Long endTime;   //结束时间
        startTime = System.currentTimeMillis();
        for (Object one : list
                )
            ;
        endTime = System.currentTimeMillis();
        long longTime = endTime - startTime;
        System.out.println("foreach:" + longTime + "ms");
    }
}
